home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18277 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.4 KB  |  134 lines

  1. Path: ras.kmit.sk!rastos
  2. From: rastos@kmit.sk (Rastislav Stanik)
  3. Newsgroups: comp.lang.c++
  4. Subject: HELP: BC 3.1 compiler bug
  5. Date: 19 Apr 1996 17:14:03 GMT
  6. Organization: Kmit
  7. Message-ID: <4l8hkr$d0s@alibaba.kmit.sk>
  8. NNTP-Posting-Host: sk2eu.eunet.sk
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. A friend of me sent me a short program which causes Borland C++ 3.1
  12. to do produce strange code. The source is at the and of this message.
  13. I compiled it myself and got the same strange result.
  14. I'd like to know if there is some patch to repair this bug or advice
  15. what to do.
  16. Any help is highly appreciated!
  17.  
  18. PROBLEM:
  19. Let's have a class SIMPLE with constructor and destructor defined.
  20. They do nothing just print out some message. And let's have a class
  21. BIG which has a constructor with one parameter of type SIMPLE.
  22. Creating an global variable of type BIG causes to invoke destructor
  23. for the passed parameter TWICE.
  24. Here is a output produced by that program (comments are mine):
  25.  
  26. SIMPLE at 8FDE:1004 created (SIMPLE & (8F95:0404))
  27.   //invoked constructor of parameter passed to constuctor of BIG
  28. simple big
  29.   //invoked constructor BIG
  30. SIMPLE at 8F95:1004 destroyed
  31.   //invoked destructor for parameter passed to constuctor of BIG
  32. SIMPLE at 8F95:1004 destroyed
  33.   //what the hell is THIS!!!
  34.  
  35. Here is the source code:
  36. --------------------------------cut here---------------------------------
  37.  /* this program produces bug under BC 3.1 */
  38.  /* can anyone help me ? */
  39.  
  40.  #include <stdio.h>
  41.  
  42.  class SIMPLE
  43.    {
  44.      public:
  45.        SIMPLE (void)
  46.          {
  47.            printf ("SIMPLE at %p created (void)\n", this);
  48.          };
  49.        SIMPLE (SIMPLE &s)
  50.          {
  51.            printf ("SIMPLE at %p created (SIMPLE & (%p))\n", this, &s);
  52.          };
  53.       ~SIMPLE (void)
  54.         {
  55.           printf ("SIMPLE at %p destroyed \n", this);
  56.         }
  57.    };
  58.  
  59.  class BIG
  60.   {
  61.     public:
  62.       BIG (SIMPLE s)
  63.         {
  64.           printf ("simple big \n");
  65.         }
  66.   };
  67.  
  68.  SIMPLE simple;
  69.  BIG big (simple);
  70.  
  71.  int main (void)
  72.    {
  73.      return 1;
  74.    }
  75.  
  76. ---------------------------------end cut here------------------------------
  77.  /* under BC 3.1: (original commmet of my friend)
  78.  SIMPLE at 8F95:0404 created (void)                  // simple created
  79.  SIMPLE at 8FDE:1004 created (SIMPLE & (8F95:0404))  // copy of simple created
  80.  simple big                                          // big created
  81.  SIMPLE at 8F95:1004 destroyed                       // copy destroyed #1
  82.  SIMPLE at 8F95:1004 destroyed                       // copy destroyed #2
  83.  SIMPLE at 8F95:0404 destroyed                       // simple destroyed
  84.  */
  85.  
  86. If you are intersted here is disassembling of critical part:
  87. (I did not checked this but I saw the above lines after my compilation)
  88.  
  89.  @_STCON_$qv    proc    far
  90.  
  91.         ...
  92.  
  93.     mov    ax,offset DGROUP:_simple        ; call constuctor for simple
  94.     push    ax
  95.     call    near ptr @@SIMPLE@$bctr$qv
  96.     pop    cx                              ; free this
  97.  
  98.     sub    sp,2                            ; alloc room for copy -----
  99.                                                                            |
  100.     mov    ax,offset DGROUP:_simple        ; create copy of simple    |
  101.     push    ax                                                         |
  102.     lea    ax,word ptr [bp-2]                                         |
  103.     push    ax                                                         |
  104.     call    near ptr @@SIMPLE@$bctr$qr6SIMPLE                          |
  105.     pop    cx                              ; free this                |
  106.     pop    cx                              ; free address of parameter|
  107.                                                                            |
  108.     mov    ax,offset DGROUP:_big           ; call constructor of big  |
  109.     push    ax                              ; which destroy copy #1    |
  110.     call    near ptr @@BIG@$bctr$q6SIMPLE   ; (it is local variable !) |
  111.     pop    cx                              ; free this                |
  112.     pop    cx                              ; free copy of simple  ____|
  113.  
  114.     mov    ax,2                            ; destroy copy #2
  115.     push    ax
  116.     lea    ax,word ptr [bp-2]              ; this address is out of stack
  117.     push    ax
  118.     call    near ptr @@SIMPLE@$bdtr$qv
  119.     pop    cx
  120.     pop    cx
  121.  
  122.         ...
  123.  
  124. @_STCON_$qv    endp
  125.  
  126. --
  127.             bye
  128.                          rastos
  129.  ___________________________________________________________________
  130. | It's not that I'm afraid to die.| PGP public key 0x21A5BF31 on    |
  131. | I just don't want to be there - | keyservers. The fingerprint is: |
  132. | when it happens.                | F2E2E1CC690778698A89A7939874DD95|
  133. `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
  134.